<?php
class ModelCatalogNews extends Model {
	public function updateViewed($news_id) {
		$this->db->query("UPDATE " . DB_PREFIX . "news SET viewed = (viewed + 1) WHERE news_id = '" . (int)$news_id . "'");
	}
	
	public function getNews($news_id) {
		
				
		$query = $this->db->query("SELECT DISTINCT *, pd.title AS title, p.image FROM " . DB_PREFIX . "news p LEFT JOIN " . DB_PREFIX . "news_description pd ON (p.news_id = pd.news_id) WHERE p.news_id = '" . (int)$news_id . "' AND pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p.status = '1' ");
		
		if ($query->num_rows) {
			
			return array(
				'news_id'       => $query->row['news_id'],
				'title'             => $query->row['title'],
				'summary'		   => $query->row['summary'],
				'description'      => $query->row['description'],
				'meta_description' => $query->row['meta_description'],
				'meta_keyword'     => $query->row['meta_keyword'],				
				'image'            => $query->row['image'],			
				'sort_order'       => $query->row['sort_order'],
				'status'           => $query->row['status'],
				'date_added'       => $query->row['date_added'],
				'date_modified'    => $query->row['date_modified'],
				'viewed'           => $query->row['viewed']
			);
		} else {
			return false;
		}
	}
	
	public function getLatestNewssByCatalogId($category_id,$limit)
	{
		$news_data=array();
		$query=$this->db->query("SELECT news_id FROM ".DB_PREFIX."news WHERE category_id='".(int)$category_id."' and status=1 ORDER BY date_added DESC LIMIT 0,".$limit."");
		foreach($query->rows as $news)
		{			
			$new=$this->getNews($news['news_id']) ;			
				$news_data[]=array(
					'title'		=>	$new['title'],
					'news_id'	=>	$new['news_id'],
					'image'		=>	$new['image'],
					'summary'	=>	$new['summary']
				);
			
		}		
		return $news_data;
	}
	
	public function getNewssByCatalogId($category_id)
	{
		$news_data=array();
		$query=$this->db->query("SELECT n.news_id FROM ".DB_PREFIX."news  WHERE category_id='".(int)$category_id."' and n.status=1 ORDER BY n.date_added DESC ");
		foreach($query->rows as $news)
		{			
			$new=$this->getNews($news['news_id']) ;			
				$news_data[]=array(
					'title'		=>	$new['title'],
					'news_id'	=>	$new['news_id'],
					'image'		=>	$new['image'],
					'summary'	=>	$new['summary']
				);
			
		}		
		return $news_data;
	}

	public function getNewss($data = array()) {
		
		
		
			$sql = "SELECT p.news_id FROM " . DB_PREFIX . "news p LEFT JOIN " . DB_PREFIX . "news_description pd ON (p.news_id = pd.news_id)  WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p.status = '1'"; 
			
			if (isset($data['filter_title']) && $data['filter_title']) {
				if (isset($data['filter_description']) && $data['filter_description']) {
					$sql .= " AND (LCASE(pd.title) LIKE '%" . $this->db->escape(mb_strtolower($data['filter_title'], 'UTF-8')) . "%' OR p.news_id IN (SELECT pt.news_id FROM " . DB_PREFIX . "news_tag pt WHERE pt.language_id = '" . (int)$this->config->get('config_language_id') . "' AND LCASE(pt.tag) LIKE '%" . $this->db->escape(mb_strtolower($data['filter_title'], 'UTF-8')) . "%') OR LCASE(pd.description) LIKE '%" . $this->db->escape(mb_strtolower($data['filter_title'], 'UTF-8')) . "%')";
				} else {
					$sql .= " AND (LCASE(pd.title) LIKE '%" . $this->db->escape(mb_strtolower($data['filter_title'], 'UTF-8')) . "%' OR p.news_id IN (SELECT pt.news_id FROM " . DB_PREFIX . "news_tag pt WHERE pt.language_id = '" . (int)$this->config->get('config_language_id') . "' AND LCASE(pt.tag) LIKE '%" . $this->db->escape(mb_strtolower($data['filter_title'], 'UTF-8')) . "%'))";
				}
			}
			
			if (isset($data['filter_tag']) && $data['filter_tag']) {
				$sql .= " AND p.news_id IN (SELECT pt.news_id FROM " . DB_PREFIX . "news_tag pt WHERE pt.language_id = '" . (int)$this->config->get('config_language_id') . "' AND LCASE(pt.tag) LIKE '%" . $this->db->escape(mb_strtolower($data['filter_tag'], 'UTF-8')) . "%')";
			}
										
			
			
			if (isset($data['filter_category_id'])) {
				$sql .= " AND p.category_id = '" . (int)$data['filter_category_id'] . "'";
			}
			
			$sql .= " GROUP BY p.news_id";
			
			$sort_data = array(
				'pd.title',			
				'p.sort_order',
				'p.date_added'
			);	
			
			if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
				if ($data['sort'] == 'pd.title' || $data['sort'] == 'p.model') {
					$sql .= " ORDER BY LCASE(" . $data['sort'] . ")";
				} else {
					$sql .= " ORDER BY " . $data['sort'];
				}
			} else {
				$sql .= " ORDER BY p.news_id";	
			}
			
			if (isset($data['order']) && ($data['order'] == 'ASC')) {
				$sql .= " ASC";
			} else {
				$sql .= " DESC";
			}
		
			if (isset($data['start']) || isset($data['limit'])) {
				if ($data['start'] < 0) {
					$data['start'] = 0;
				}				
	
				if ($data['limit'] < 1) {
					$data['limit'] = 20;
				}	
			
				$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
			}
			
			$news_data = array();
			
			$query = $this->db->query($sql);
			
			foreach ($query->rows as $result) {
				$news_data[$result['news_id']] = $this->getNews($result['news_id']);
			}			
			return $news_data;
		
	}
	
	
		
	public function getLatestNewss($limit) {
		$news_data = $this->cache->get('news.latest.' . $this->config->get('config_language_id') . '.' . (int)$this->config->get('config_store_id') . '.' . $limit);

		if (!$news_data) { 
			$query = $this->db->query("SELECT p.news_id FROM " . DB_PREFIX . "news p LEFT JOIN " . DB_PREFIX . "news_description pd ON (p.news_id = pd.news_id) WHERE p.status = '1' AND p.promotion='0' AND pd.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY p.date_added DESC LIMIT " . (int)$limit);
		 	 
			foreach ($query->rows as $result) {
				$news_data[$result['news_id']] = $this->getNews($result['news_id']);
			}
			
			$this->cache->set('news.latest.' . $this->config->get('config_language_id') . '.' . (int)$this->config->get('config_store_id') . '.' . $limit, $news_data);
		}
		
		return $news_data;
	}
	
	
	
	public function getPopularNewss($limit) {
		$news_data = array();
		
		$query = $this->db->query("SELECT p.news_id FROM " . DB_PREFIX . "news p LEFT JOIN " . DB_PREFIX . "news_to_store p2s ON (p.news_id = p2s.news_id) WHERE p.status = '1' AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "' ORDER BY p.viewed, p.date_added DESC LIMIT " . (int)$limit);
		
		foreach ($query->rows as $result) { 		
			$news_data[$result['news_id']] = $this->getNews($result['news_id']);
		}
					 	 		
		return $news_data;
	}

	
			
	
	public function getNewsImages($news_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "news_image WHERE news_id = '" . (int)$news_id . "'");

		return $query->rows;
	}
	
	public function getNewsRelated($news_id) {
		$news_data = array();

		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "news_related pr LEFT JOIN " . DB_PREFIX . "news p ON (pr.related_id = p.news_id) LEFT JOIN " . DB_PREFIX . "news_to_store p2s ON (p.news_id = p2s.news_id) WHERE pr.news_id = '" . (int)$news_id . "' AND p.status = '1' AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "'");
		
		foreach ($query->rows as $result) { 
			$news_data[$result['related_id']] = $this->getNews($result['related_id']);
		}
		
		return $news_data;
	}
		
	public function getNewsTags($news_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "news_tag WHERE news_id = '" . (int)$news_id . "' AND language_id = '" . (int)$this->config->get('config_language_id') . "'");

		return $query->rows;
	}
		
	public function getNewsLayoutId($news_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "news_to_layout WHERE news_id = '" . (int)$news_id . "' AND store_id = '" . (int)$this->config->get('config_store_id') . "'");
		
		if ($query->num_rows) {
			return $query->row['layout_id'];
		} else {
			return  $this->config->get('config_layout_news');
		}
	}
	
	public function getCategories($news_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "news_to_category WHERE news_id = '" . (int)$news_id . "'");
		
		return $query->row;
	}	
		
	public function getTotalNewss($data = array()) {
		$sql = "SELECT COUNT(DISTINCT p.news_id) AS total FROM " . DB_PREFIX . "news p LEFT JOIN " . DB_PREFIX . "news_description pd ON (p.news_id = pd.news_id) WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p.status = '1' ";
		
		if (isset($data['filter_title'])) {
			if (isset($data['filter_description']) && $data['filter_description']) {
				$sql .= " AND (LCASE(pd.title) LIKE '%" . $this->db->escape(mb_strtolower($data['filter_title'], 'UTF-8')) . "%' OR p.news_id IN (SELECT pt.news_id FROM " . DB_PREFIX . "news_tag pt WHERE pt.language_id = '" . (int)$this->config->get('config_language_id') . "' AND LCASE(pt.tag) LIKE '%" . $this->db->escape(mb_strtolower($data['filter_title'], 'UTF-8')) . "%') OR LCASE(pd.description) LIKE '%" . $this->db->escape(mb_strtolower($data['filter_title'], 'UTF-8')) . "%')";
			} else {
				$sql .= " AND (LCASE(pd.title) LIKE '%" . $this->db->escape(mb_strtolower($data['filter_title'], 'UTF-8')) . "%' OR p.news_id IN (SELECT pt.news_id FROM " . DB_PREFIX . "news_tag pt WHERE pt.language_id = '" . (int)$this->config->get('config_language_id') . "' AND LCASE(pt.tag) LIKE '%" . $this->db->escape(mb_strtolower($data['filter_title'], 'UTF-8')) . "%'))";
			}
		}
		
		if (isset($data['filter_tag']) && $data['filter_tag']) {
			$sql .= " AND p.news_id IN (SELECT pt.news_id FROM " . DB_PREFIX . "news_tag pt WHERE pt.language_id = '" . (int)$this->config->get('config_language_id') . "' AND LCASE(pt.tag) LIKE '%" . $this->db->escape(mb_strtolower($data['filter_tag'], 'UTF-8')) . "%')";
		}								
		
		
		if (isset($data['filter_category_id'])) {
			$sql .= " AND p.category_id = '" . (int)$data['filter_category_id'] . "'";
		}
				
		$query = $this->db->query($sql);
		
		return $query->row['total'];
	}
	
	
			
	
}
?>